#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash kml-1.6.0.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar environment-modules libatomic cpio

kml_is_exist=`rpm -qa kml`

if [[ "$kml_is_exist" = "" ]];then
  wget -t 40 -c -P $buildpath https://kunpeng-repo.obs.cn-north-4.myhuaweicloud.com/Kunpeng%20BoostKit/Kunpeng%20BoostKit%2022.0.RC3/BoostKit-kml_1.6.0_bisheng.zip
  cd $buildpath
  unzip BoostKit-kml_1.6.0_bisheng.zip
  #rpm -ivh boostkit-kml-1.6.0-1.aarch64.rpm
  rpm2cpio boostkit-kml-1.6.0-1.aarch64.rpm |cpio -div
  cp -r ./usr/local/kml $installpath/ && rm -rf ./usr/local/kml
  #rm -rf $buildpath/BoostKit-kml_1.6.0_bisheng.zip && rm -rf $buildpath/boostkit-kml-1.6.0-1.aarch64.rpm
else
  echo "kml is exist, not need to install!"
  cp -r /usr/local/kml $installpath/
fi

cat > "$installpath/kml/kml_modulefiles" <<EOF
#%Module1.0
conflict kml
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set KML \$pwd
setenv KML \$KML
prepend-path LD_LIBRARY_PATH \$KML/lib
prepend-path INCLUDE \$KML/include
EOF
